// Copyright (c) 2002 Cunningham & Cunningham, Inc.
// Read license.txt in this directory.

package eg.music
{

public class MusicPlayer {

    public static var playing : Music = null;
    private static var paused : Number = 0;

    // Controls /////////////////////////////////

    public static function play(m : Music) : void {
        if (paused == 0) {
            Music.status = "loading";
            var seconds : Number = m == playing ? 0.3 : 2.5 ;
            Simulator.nextPlayStarted = Simulator.schedule(seconds);
        } else {
            Music.status = "playing";
            Simulator.nextPlayComplete = Simulator.schedule(paused);
            paused = 0;
        }
    }

    public static function pause() : void {
        Music.status = "pause";
        if (playing != null && paused == 0) {
            paused = (Simulator.nextPlayComplete - Simulator.time) / 1000.0;
            Simulator.nextPlayComplete = 0;
        }
    }

    public static function stop() : void {
        Simulator.nextPlayStarted = 0;
        Simulator.nextPlayComplete = 0;
        playComplete();
    }

    // Status ///////////////////////////////////

    public static function secondsRemaining() : Number {
        if (paused != 0) {
            return paused;
        } else if (playing != null) {
            return (Simulator.nextPlayComplete - Simulator.time) / 1000.0;
        } else {
            return 0;
        }
    }

    public static function minutesRemaining() : Number {
        return Math.round(secondsRemaining() / .6) / 100.0;
    }

    // Events ///////////////////////////////////

    public static function playStarted() : void {
        Music.status = "playing";
        playing = MusicLibrary.instance.looking;
        Simulator.nextPlayComplete = Simulator.schedule(playing.seconds);
    }

    public static function playComplete() : void {
        Music.status = "ready";
        playing = null;
    }
}
}